//
//  YFBrowseRecordsVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/5/25.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFBrowseRecordsVC.h"
#import "YFBrowseRecordCell.h"
#import "QJShopDetailVC.h"

@interface YFBrowseRecordsVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation YFBrowseRecordsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"我的足迹";
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [WWNetRequest fetchBrowseRecordsWithParameters:nil success:^(id response) {
        NSLog(@"我的足迹 response = %@", response);
        self.dataArr = nil;
        NSArray *infoArr = response[@"data"][@"visit_log"];
        for (NSDictionary *dict in infoArr) {
            YFBrowseRecordModel *model =  [[YFBrowseRecordModel alloc] initWithDictionary:dict error:nil];
            [self.dataArr addObject:model];
        }
        [self.tableView reloadData];
        
    } failure:^(NSDictionary *error) {
        NSLog(@"我的足迹 error = %@", error);
    }];
}

#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YFBrowseRecordCell *cell = [YFBrowseRecordCell cellWithTableView:tableView];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    QJShopDetailVC *VC = [QJShopDetailVC new];
    YFBrowseRecordModel *model = self.dataArr[indexPath.row];
    VC.goodsID = [NSString stringWithoutNilFormart:model.goods_id];
    KPushViewControllerAction(VC);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

#pragma mark - 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
//        CGFloat top = IS_iOS11Later ? Height_NavBar : 0;
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT )];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

@end
